package models

import (
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"gowww/comeon/config/dev"
)

type Admin struct {
	Id        int    `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT;comment:'id'"`
	Username  string `json:"username" gorm:"column:username;type:char(10)"`
	Password  string `json:"password" gorm:"column:password;type:char(32)"`
	Status    uint8  `json:"status" gorm:"column:status;default:1"`
	Type      uint8  `json:"type" gorm:"column:type;default:0"`
	CreatedAt int64  `json:"created_at" gorm:"column:created_at"`
	UpdatedAt int64  `json:"updated_at" gorm:"column:updated_at"`
}

const STATUS_ON = 1

/**
 * 自定义表名
 * return string
 */
func (Admin) TableName() string {
	return "sy_admin"
}

func (admin *Admin) Add() (id int, err error) {
	result := dev.DB.Table(admin.TableName()).Create(admin)
	id = admin.Id
	if result.Error != nil {
		err = result.Error
		return
	}
	return
}

func (admin *Admin) GetOne() (ad *Admin, err error) {
	err = dev.DB.Debug().Table(admin.TableName()).Where("username = ? and status = ?", admin.Username, STATUS_ON).Find(&admin).Error
	return admin, err
}
